package com.videoaggre.zcl

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Process
import com.bumptech.glide.request.target.ViewTarget
import com.kk.taurus.ijkplayer.IjkPlayer
import com.kk.taurus.playerbase.config.PlayerConfig
import com.kk.taurus.playerbase.config.PlayerLibrary
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.tencent.smtt.export.external.TbsCoreSettings
import com.tencent.smtt.sdk.QbSdk
import com.videoaggre.zcl.common.toastMessageLong
import com.videoaggre.zcl.utils.ActivityManger
import com.videoaggre.zcl.utils.GlideAlbumLoader
import com.videoaggre.zcl.utils.Logger
import com.yanzhenjie.album.Album
import com.yanzhenjie.album.AlbumConfig
import java.util.*
import kotlin.collections.HashMap


class App : Application() {

    companion object {
        lateinit var app: Application

        init {
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
                layout.setPrimaryColorsId(R.color.white, R.color.text_color) //全局设置主题颜色
                ClassicsHeader(context)
            }
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
                layout.setPrimaryColorsId(R.color.white, R.color.text_color) //全局设置主题颜色
                ClassicsFooter(context).setDrawableSize(20f)
            }
        }
    }

    override fun onCreate() {
        super.onCreate()
        val processName = getProcessName(this)
        if (packageName == processName) {
            init()
        }
    }

    private fun init() {
        app = this
        ActivityManger.registerActivityLifecycleCallbacks(this)
//        Logger.isDebug = BuildConfig.DEBUG;
//        koinInit(this)
        ViewTarget.setTagId(R.id.glide_tag)
        Album.initialize(
            AlbumConfig.newBuilder(this)
                .setAlbumLoader(GlideAlbumLoader()) // 设置Album加载器。
                .setLocale(Locale.CHINA) // 比如强制设置在任何语言下都用中文显示。
                .build()
        )
        PlayerConfig.setUseDefaultNetworkEventProducer(true)
        IjkPlayer.init(this)
        val map = HashMap<String, Any>()
        map[TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER] = true
        map[TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE] = true
        QbSdk.initTbsSettings(map)
        QbSdk.initX5Environment(this, object : QbSdk.PreInitCallback {
            override fun onCoreInitFinished() {
                Logger.e("onCoreInitFinished")
            }

            override fun onViewInitFinished(init: Boolean) {
                if (!init) {
                    "内核初始化失败，可尝试杀掉APP重启".toastMessageLong()
                }
                Logger.e("init==>${init}")
            }
        })
    }

    private fun getProcessName(context: Context): String? {
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val runningApps = am.runningAppProcesses ?: return null
        for (proInfo in runningApps) {
            if (proInfo.pid == Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName
                }
            }
        }
        return null
    }
}